package com.artisan.common.utils.wxpay;


import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class SignUtil {
	public static String getPayCustomSign(Map<String, String> bizObj, String key)
			throws Exception {
		String bizString = CommonUtil.FormatBizQueryParaMap(bizObj, false);
		String stringA = "appid=" + bizObj.get("appid") + "&body="
				+ bizObj.get("body") + "&device_info="
				+ bizObj.get("device_info") + "&mch_id=" + bizObj.get("mch_id")
				+ "&nonce_str=" + bizObj.get("nonce_str") + "&notify_url="
				+ PayConfig.WeixinGZHPayConfig.NOTIFY_URL + "&openid=" + bizObj.get("openid")
				+ "&out_trade_no=" + bizObj.get("out_trade_no")
				+ "&spbill_create_ip=" + bizObj.get("spbill_create_ip")
				+ "&total_fee=" + bizObj.get("total_fee") + "&trade_type=JSAPI";
		return MD5SignUtil.sign(stringA, key);
	}
	
	public static String getPayCustomSign2(Map<String, String> bizObj, String key)
			throws Exception {
		String bizString = CommonUtil.FormatBizQueryParaMap(bizObj, false);
		String stringA = "appId=" + bizObj.get("appId") + "&nonceStr=" + bizObj.get("nonceStr") 
				
				+ "&package=" + bizObj.get("packageValue")
				+ "&signType=" + bizObj.get("signType") + "&timeStamp="+ bizObj.get("timeStamp");
		return MD5SignUtil.sign(stringA, key);
	}


	/**
	 * 生成MD5签名
	 * @param packageParams
	 * @param merchantKey
	 * @return
	 */
	public static String signMd5_2(Map<String, String> packageParams, String merchantKey) {
		StringBuffer sb = new StringBuffer();
		/*Set es = packageParams.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}*/
		sb.append("appid=").append((String)packageParams.get("appid"))
				.append("&mch_id=").append((String)packageParams.get("mch_id"))
				.append("&nonce_str=").append((String)packageParams.get("nonce_str"))
				.append("&out_refund_no=").append((String)packageParams.get("out_refund_no"))
				.append("&out_trade_no=").append((String)packageParams.get("out_trade_no"))
				.append("&refund_fee=").append((String)packageParams.get("refund_fee"))
				.append("&total_fee=").append((String)packageParams.get("total_fee"))
				.append("&key=" + merchantKey);
		String sign = MD5Util.MD5(sb.toString()).toUpperCase();
		return sign;
	}
}
